package com.zc.im.service.user.impl;

import com.zc.im.mapper.user.ZcAdminUserMapper;
import com.zc.im.model.customer.ZcCustomerService;
import com.zc.im.model.user.ZcAdminUser;
import com.zc.im.model.user.ZcAdminUserKey;
import com.zc.im.service.customer.CustomerService;
import com.zc.im.service.user.ZcAdminUseService;
import com.zc.im.util.ImGolbalTypeContext;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by wangcc on 2017/7/22.
 */
@Service
public class ZcAdminUserServiceImpl implements ZcAdminUseService {

    @Autowired
    private ZcAdminUserMapper zcAdminUserMapper;
    @Autowired
    private CustomerService customerService;

    private Logger logger = Logger.getLogger(ZcAdminUserServiceImpl.class);

    /**
     * 查询客服信息
     * @param zcAdminUserId
     * @return
     */
    @Override
    public ZcAdminUser findZcAdminUser(Integer zcAdminUserId) {
        ZcAdminUser zcAdminUser = new ZcAdminUser();
        if (zcAdminUserId==null) {
            logger.error("find zcAdminUserId is null");
            return zcAdminUser;
        }
        ZcAdminUserKey zcAdminUserKey = new ZcAdminUserKey();
        zcAdminUserKey.setUid(zcAdminUserId);
        try {
            zcAdminUser = zcAdminUserMapper.selectByPrimaryKey(zcAdminUserKey);
        }catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return zcAdminUser;
    }
    /**
     * 添加一个客服分配
     * @param zcAdminUserId
     * @return
     */
    @Override
    public Integer  saveCoustormerService(ZcAdminUser zcAdminUserId) {
        Integer saveCoustormerService = null;
        if (zcAdminUserId==null) {
            logger.error("save CoustormerService is null");
            return saveCoustormerService;
        }
        try {
            saveCoustormerService =  saveZcCoustormerService(zcAdminUserId);

        }catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return saveCoustormerService;
    }

    /**
     * 删除客服信息
     * @param zcAdminUserId
     * @return
     */
    @Override
    public Integer delectCoustormerService(Integer zcAdminUserId) {
        Integer zcAdminUserIdKey=null;
        if (zcAdminUserId == null) {
            return zcAdminUserId;
        }
        try {
            zcAdminUserIdKey = customerService.deleteCustomerServiceUid(zcAdminUserId);
        }catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return zcAdminUserIdKey;
    }

    /**
     * 组装客服分配信息添加
     * @param zcAdminUser
     * @return
     */
    private Integer saveZcCoustormerService(ZcAdminUser zcAdminUser) {
        Integer saveZcCustomerServiceKey=null;
        ZcCustomerService zcCustomerService = new ZcCustomerService();
        if (zcAdminUser==null) {
            logger.error("save ZcCustomerService is null");
            return saveZcCustomerServiceKey;
        }
        zcCustomerService.setUserId(zcAdminUser.getUid());
        zcCustomerService.setCustomerServiceName(zcAdminUser.getUserName());
        zcCustomerService.setServiceUserNum( 0 );
        zcCustomerService.setAddr(zcAdminUser.getAddress());
        if (zcAdminUser.getGroupId() == 3 ){
            //添加个人
            zcCustomerService.setCustomerServiceType(ImGolbalTypeContext.IM_GROUP_TYPE_PEOPLE);
        } else  if(zcAdminUser.getGroupId() == 12 ) {
            //添加企业
            zcCustomerService.setCustomerServiceType(ImGolbalTypeContext.IM_GROUP_TYPE_COMPANY);
        } else {
            logger.error("save  customerService   coustormerTpye is null");
            return saveZcCustomerServiceKey;
        }
        try {
            saveZcCustomerServiceKey = customerService.insertCustomerService( zcCustomerService );
        }catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return saveZcCustomerServiceKey;
    }

}
